# class Solution(object):
#     def maximumSwap(self, num):
#         """
#         :type num: int
#         :rtype: int
#         """
#         s = str(num)
#         n = len(s)
#         max_v = num
#         for i in range(n):
#             for j in range(i + 1, n):
#                 val = int(s[: i] + s[j] + s[i + 1: j] + s[i] + s[j + 1:])
#                 max_v = max(val, max_v)
#         return max_v

class Solution(object):
    def maximumSwap(self, num):
        """
        :type num: int
        :rtype: int
        """
        s = str(num)
        n = len(s)
        d = {}
        for i in range(n):
            d[s[i]] = i
        for i in range(n):
            for key in ['9','8','7','6','5','4','3','2','1']:
                if key in d:
                    if key > s[i] and d[key] > i:
                        return int(s[:i] + key + s[i + 1: d[key]] + s[i] + s[d[key] + 1:])
        return num

data = Solution()
num = 123
print(data.maximumSwap(num))
